var express = require('express');
var router = express.Router();
var menudao = require('../dao/menudao');
var utils = require('../common/utils');

router.get('/list', function (req, res, next) {
    var userid = req.session.user.userid;
    var guestid = req.query.guestid;
    menudao.list(userid, guestid).then(function (data) {
        var item = [];
        data.forEach(element => {
            item.push(element.parentname);
        });
        res.json({
            "code": 0,
            "msg": "",
            data: data[0],
            items: utils.unique(item)
        });
    }, function (error) {
        res.json({
            "code": 1,
            "msg": error.message
        });
    })
});

router.post('/permissionchange', function (req, res, next) {
    var userid = req.body.userid;
    var menuid = req.body.menuid;
    var status = req.body.status;
    var params={};
    if (status == 1) {
        params.id=utils.generalUUID();
        params.userid=userid;
        params.menuid=menuid;
        menudao.addpermission(params).then(function (data) {
            res.json({
                "code": 1,
                "msg": ""
            });
        }, function (error) {
            res.json({
                "code": 0,
                "msg": error.message
            });
        })
    }else if(status==0){        
        params.userid=userid;
        params.menuid=menuid;
        menudao.delpermission(params).then(function (data) {
            res.json({
                "code": 1,
                "msg": ""
            });
        }, function (error) {
            res.json({
                "code": 0,
                "msg": error.message
            });
        })
    }
});
module.exports = router;